package com.huiyuanai.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @Author: liwan
 * @Description:
 * @Date: create in 2021/10/3
 */
public class BaseUtil {

    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    //静态代码块
    static {
        Properties properties = new Properties();
        //通过类加载器读取资源
        InputStream resourceAsStream = BaseUtil.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //读取资源
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
    }

    //数据库连接
    public static Connection getConnectionDIY() throws Exception{
        //加载驱动
        Class.forName(driver);
        //连接数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }

    //公共代码查询部分              这里为什么放这么参数   ！！！   方便一起close
    public static ResultSet execute(Connection connection, String sql, Object[] datas, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        //给参数
        for (int i = 0; i < datas.length; i++) {
            preparedStatement.setObject(i + 1, datas[i]);
        }
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }

    //公共代码增 删 改部分
    public static int execute(Connection connection, String sql, Object[] datas, PreparedStatement preparedStatement) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        //给参数
        for (int i = 0; i < datas.length; i++) {
            preparedStatement.setObject(i + 1, datas[i]);
        }
        int updateRows = preparedStatement.executeUpdate();
        return updateRows;
    }

    //释放资源
    public static boolean clostResour(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        boolean flag = true;
        if (resultSet != null) {
            try {
                resultSet.close();
                //GC回收
                resultSet = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                //GC回收
                preparedStatement = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }
        if (connection != null) {
            try {
                connection.close();
                //GC回收
                connection = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }
        return flag;
    }
}
